package cn.swust.designpatternlearningplatform.domain.dto;

import cn.swust.designpatternlearningplatform.domain.bo.NodeBo;
/**
 * @author wang
 * description 树型结构
 * date 2023/4/18 22:01
 */
public class TreeDto {
    //节点
    private NodeBo root;

    public TreeDto(NodeBo root) {
        this.root = root;
    }

    public NodeBo getRoot() {
        return root;
    }

    public void setRoot(NodeBo root) {
        this.root = root;
    }
   //通过id获取节点
    public NodeBo getNodeById(int id) {
        return getNodeById(root, id);
    }

    private NodeBo getNodeById(NodeBo node, int id) {
        if (node.getId() == id) {
            return node;
        }
        for (NodeBo child : node.getChildren()) {
            NodeBo found = getNodeById(child, id);
            if (found != null) {
                return found;
            }
        }
        return null;
    }
}
